package com.comdo.newup_loan.module.myinfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.comdo.newup_loan.MyApplication;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.IndustryAdapter;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.IndustryBean;
import com.comdo.newup_loan.util.TitleMenuUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2018/1/8.
 */

public class IndustryActivity extends BaseActivity<IndustryPersenter> implements IndustryContact.View{
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private ArrayList<IndustryBean> listData = new ArrayList<IndustryBean>();
    private IndustryAdapter mAdapter;
    private int type;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_industry;
    }

    @Override
    protected void init() {
        MyApplication.getInstance().addInDustryActivities(this);
        super.init();

    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        type=getIntent().getIntExtra("type",-1);
        Log.e("type",type+"");
        if(type==0){
            mPresenter.getList();
        }else{
            mPresenter.getOtherList(getIntent().getStringExtra("industryCode"));
        }
        new TitleMenuUtil(IndustryActivity.this, "第"+(type+1)+"级行业列表", false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(IndustryActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        listData = new ArrayList<IndustryBean>();
        mAdapter = new IndustryAdapter(listData, IndustryActivity.this);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new IndustryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(type==3){
                    Config.industryBean=listData.get(position);
                    MyApplication.getInstance().finishAll1();
                    IndustryActivity.this.finish();

                }else{
                    int index=type+1;
                    Intent intent=new Intent(IndustryActivity.this,IndustryActivity.class);
                    intent.putExtra("type",index);
                    intent.putExtra("industryCode",listData.get(position).getIndustryCode());
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    public IndustryPersenter getmPresenter() {
        return new IndustryPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getList(List<IndustryBean> datas) {
        listData.clear();
        listData.addAll(datas);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getOtherList(List<IndustryBean> datas) {
        listData.clear();
        listData.addAll(datas);
        mAdapter.notifyDataSetChanged();
    }

}
